import { useAuth } from '@clerk/nextjs'

export type OptionsType =
  | {
      method?:
        | 'GET'
        | 'POST'
        | 'PUT'
        | 'DELETE'
        | 'PATCH'
        | 'HEAD'
        | 'OPTIONS'
        | 'post'
        | 'get'
        | 'delete'
        | 'put'
        | 'patch'
        | 'options'
        | 'head'
      headers: HeadersInit | undefined
    }
  | undefined

export default async function request(
  url: string,
  params: any,
  options?: OptionsType
) {
  const auth: {} = localStorage.getItem('token')
    ? { Authorization: `Bearer ${localStorage.getItem('token')}` }
    : {}

  const requestInit: RequestInit = {
    method: options?.method || 'POST',
    mode: 'cors',
    headers: {
      'Content-Type': 'application/json',
      ...auth,
      ...options?.headers
    },
    body: JSON.stringify(params)
  }
  const response = await fetch(url, requestInit)

  return response.json()
}
